clear all 
capture log close


global data "/Users/ceweber/Dropbox/JPAM_TIV_code/original_data/"
global dta "/Users/ceweber/Dropbox/JPAM_TIV_code/dta_files/"
global dataclean "/Users/ceweber/Dropbox/JPAM_TIV_code/dta_output/"
global output "/Users/ceweber/Dropbox/JPAM_TIV_code/output/"

 
set more off
cd "${data}"

use "supply_chain_summary.dta", clear

mmerge parentlot using ${data}parentlot_time.dta, type(n:1) unmatched(master)  

gen mix=prodorg=="Mixed"
destring prodorg, force replace
replace prodorg=1 if mix==1 /*keep in parentlots that come from more than one producer, but do need to be careful with these -- producer FE doesn't make a lot of sense here*/

gen usableweightwh=usable_weight*weight_original /*this is total weight of the sale*/
gen pricepergramwh=unitprice_wholesale / usable_weight
gen totalsaleswh=unitprice_wholesale*weight_original

*get rid of duplicates/multiple sales of the same item:
egen tag=tag(inventoryid salesorg salesloc)
egen meantag=mean(tag), by(inventoryid salesorg salesloc)
egen  maxprice=max(pricepergramwh), by(inventoryid salesorg salesloc)

drop if meantag<1&pricepergramwh<maxprice
drop tag
egen tag=tag(inventoryid salesorg salesloc)
drop if tag==0
drop tag

gen childdate=trunc(dofc(first_child_seen))
gen childdate2=childdate+1
gen childweekly=wofd(childdate2)

 drop if pricepergramwh<=.01
drop if totalsaleswh<=.01
drop if unitprice_wholesale<=.01

rename proclog procloc
keep inventoryid salesorg salesloc procloc procorg prodorg parentlot pricepergramwh date_wholesale strain productname  type usableweightwh  strain potency* totalsaleswh childweekly usable_weight weight_original unitprice_wholesale

count 
drop if strain==""
cd "${dta}"
mmerge strain using "strain-clean-map.dta", ukeep(strain_final) type(n:1) unmatched(master) 
cd "${data}"
*old file: strain-map-post-cleaning
rename strain_final strains
drop strain 
drop if strains==""

*generate a pre-roll indicator:
gen tmp3=lower(productname)
replace productname=tmp3

gen pre="pre"
gen preroll=strpos(productname, pre)
gen roll="roll"
gen tmp1=strpos(productname, roll)
replace preroll=tmp1 if preroll==0&tmp1>=1&tmp1<.
gen joint="joint"
gen tmp2=strpos(productname, joint)
replace preroll=tmp2 if preroll==0&tmp2>=1&tmp2<.
drop tmp2
gen fattie="fattie"
gen tmp2=strpos(productname, fattie)
replace preroll=tmp2 if preroll==0&tmp2>=1&tmp2<.
drop tmp2
gen smalls="smalls"
gen tmp2=strpos(productname, smalls)
replace preroll=tmp2 if preroll==0&tmp2>=1&tmp2<.
drop tmp2
gen spliff="spliff"
gen tmp2=strpos(productname, spliff)
replace preroll=tmp2 if preroll==0&tmp2>=1&tmp2<.
drop tmp2
gen cone="cone"
gen tmp2=strpos(productname, cone)
replace preroll=tmp2 if preroll==0&tmp2>=1&tmp2<.
drop tmp2
gen pinecone="pinecone"
gen tmp2=strpos(strains, pinecone)
replace preroll=0 if preroll==0&tmp2>=1&tmp2<.

gen pr="pr"
gen tmp4=strpos(productname, pr)
gen pra="pra"
gen tmp5=strpos(productname, pra)
replace tmp4=0 if tmp5>0&tmp5<.
gen pro="pro"
gen tmp6=strpos(productname, pro)
replace tmp4=0 if tmp6>0&tmp6<.
gen prf="prf"
gen tmp7=strpos(productname, prf)
replace tmp4=0 if tmp7>0&tmp7<.
gen spr="spr"
gen tmp8=strpos(productname, spr)
replace tmp4=0 if tmp8>0&tmp8<.
gen pres="pres"
gen tmp9=strpos(productname, pres)
replace preroll=0 if tmp9>0&tmp9<.
gen pri="pri"
gen tmp10=strpos(productname, pri)
replace tmp4=0 if tmp10>0&tmp10<.
drop tmp10
gen prm="prm"
gen tmp10=strpos(productname, prm)
replace tmp4=0 if tmp10>0&tmp10<.
drop tmp10
gen prp="prp"
gen tmp10=strpos(productname, prp)
replace tmp4=0 if tmp10>0&tmp10<.
drop tmp10
gen prem="prem"
gen tmp10=strpos(productname, prem)
replace preroll=0 if tmp10>0&tmp10<.
drop tmp10
replace preroll=tmp4 if tmp4>0&tmp4<.

drop roll-prem pre

gen potency_thctotal= potency_thca* 0.877 +potency_thc

   egen tag=tag(inventoryid)
 count if tag
 drop tag
  
 //cut off vars at the very top
 replace potency_thctotal=. if potency_thctotal==0
 
centile potency_thctotal if (type==28|type==31|type==32), c(99.5)
replace potency_thctotal=. if potency_thctotal>r(c_1)&(type==28|type==31|type==32)
centile potency_thctotal if type==24, c(99.5)
replace potency_thctotal=. if potency_thctotal>r(c_1)&type==24
centile potency_thctotal if (type==25|type==37), c(99.9)
replace potency_thctotal=. if potency_thctotal>r(c_1)& (type==25|type==37)

centile pricepergramwh if (type==28|type==31|type==32), c(99.99)
 drop if pricepergramwh>r(c_1)&usableweight<.&(type==28|type==31|type==32)
 centile usableweightwh if (type==28|type==31|type==32), c(99.99)
 drop if usableweightwh>r(c_1)&usableweight<.&(type==28|type==31|type==32) 
 centile totalsaleswh if (type==28|type==31|type==32), c(99.99)
  drop if totalsaleswh>r(c_1)&(type==28|type==31|type==32) 
   centile totalsaleswh if type==24, c(99.99)
  drop if totalsaleswh>r(c_1)&type==24
   centile totalsaleswh if (type==25|type==37), c(99.99)
  drop if totalsaleswh>r(c_1)&(type==25|type==37)
    centile totalsaleswh if   type==22|type==23|type==34|type==35, c(99.99)
  drop if totalsaleswh>r(c_1)&  type==22|type==23|type==34|type==35

drop if (type==28|type==31|type==32)&potency_thctotal>40&potency_thctotal<.

   egen tag=tag(inventoryid)
 count if tag
 drop tag
 
 replace potency_thctotal=. if potency_thctotal==0
replace potency_thctotal=. if potency_thctotal>=40
 drop if pricepergramwh>42
 drop if usableweightwh>2500
 *no clear cut at the low end of the price or weight distribution.
 
*convert from %tC format to day (this is day sold from processor to retailer):
gen datewh=trunc(dofc(date_wholesale))
format datewh %d

gen month=month(datewh)
gen year=year(datewh)
gen datewh2=datewh+1
gen weekly=wofd(datewh2)
format weekly %tw
gen monthly=mofd(datewh)

summ weekly

*generate indicator for whether sales coming from vertically integrated producers-processors:
gen vertical=0
replace vertical=1 if prodorg==procorg
replace vertical=0 if prodorg==1 /*mixed*/
replace vertical=. if prodorg==.

summ vertical
 
gen sale=1
gen verticalweight=vertical*usableweightwh

egen prodorgs=tag(prodorg procorg)

gen inventorytype=.
replace inventorytype=1 if type==28|(type==31)|type==32 //hammers, rocks, cones, pre rolls etc, were all to a large degree part of type 28 before (note, for example that a large number of rocks in type 22, but I think the rocks that are in 32 used to be in 28 based on volume changes)
*(type==31&preroll==1) 
replace inventorytype=2 if type==22|type==23|type==34|type==35 //capsules were in 22 or 23 before 34 was a cateogory. tinctures were type 23 before 35 was a category.
replace inventorytype=3 if type==24
replace inventorytype=4 if type==25|type==37 //capsules used to be included in topicals
 
gen meanweight=usableweightwh

egen modeminpricepergramwh=mode(pricepergramwh), minmode by(parentlot inventorytype)
egen modemaxpricepergramwh=mode(pricepergramwh), maxmode by(parentlot inventorytype)

egen maxpricepergramwh=max(pricepergramwh), by(salesloc procloc strains vertical parentlot weekly)
gen meanmaxweight=usableweightwh if pricepergramwh==maxpricepergramwh 
egen maxpricepergramwh2=max(pricepergramwh), by(salesloc procloc strains vertical parentlot meanweight weekly)

gen tmpx=0
replace tmpx=1 if pricepergramwh/modemaxpricepergramwh>10&modemax<.
replace tmpx=1 if pricepergramwh/modeminpricepergramwh<.1
egen dropclean=max(tmpx), by(parentlot inventorytype) 

drop if parentlot ==""
cd "${dataclean}"
save full_wholesale_clean_forincidence.dta, replace /*just prior to collapse*/

*CREATE FINAL WEEKLY DATA SET:
cd "${dataclean}"
use full_wholesale_clean_forincidence.dta, clear /*just prior to collapse*/
drop tmpx
egen firstparentlotdate=min(weekly), by(parentlot)
gen tmpx= pricepergramwh if firstparentlotdate==weekly
egen meanpricewhfirst=mean(tmpx), by(parentlot)
drop tmpx
gen tmpx=pricepergramwh if weekly!=firstparentlotdate
egen meanpricewh=mean(tmpx) , by(parentlot)
drop if meanpricewh/meanpricewhfirst>=10& meanpricewh/meanpricewhfirst<.&abs(meanpricewhfirst-pricepergramwh)<.5
gen gap=meanpricewh/meanpricewhfirst>=3&meanpricewh/meanpricewhfirst<10
drop tmpx

gen logpricewh=log(pricepergramwh)

egen tmpx=total(logpricewh*usableweightwh), by(weekly salesloc procloc strains parentlot vertical)
egen tmpx2=total(usableweightwh), by(weekly salesloc procloc strains parentlot vertical)
gen logpricewh_wgt=tmpx/tmpx2

drop tmpx*
egen tmpx=total(pricepergramwh*usableweightwh), by(weekly salesloc procloc strains parentlot vertical)
egen tmpx2=total(usableweightwh), by(weekly salesloc procloc strains parentlot vertical)
gen pricepergramwh_wgt=tmpx/tmpx2

*merge in inventory the day before each sale.
gen datewhm1=datewh-1
cd "${data}"
mmerge inventoryid salesloc salesorg datewhm1  using  "inventory_by_date.dta", type(n:1) unmatched(master) umatch(inventoryid salesloc salesorg date) ukeep(weight_at_start_of_day) urename(weight_at_start_of_day invweightdaybefore)

*merge in inventory the day before 7/1/2015
gen date630=20270-1
mmerge inventoryid salesloc salesorg date630  using  "inventory_by_date.dta", type(n:1) unmatched(master) umatch(inventoryid salesloc salesorg date) ukeep(weight_at_start_of_day) urename(weight_at_start_of_day invweight0630)

gen ppgl1=pricepergramwh<1
gen ppglp5=pricepergramwh<.5
drop if pricepergramwh<1 

gen round1=round(pricepergramwh,1)==pricepergramwh
gen round1_whole=round(totalsaleswh,1)==totalsaleswh

*pctgapbysaleday pctgapbysaleday_adjusted
collapse (sum) sale totalsaleswh usableweightwh verticalweight (mean) round1 round1_whole invweight0630 invweightdaybefore gap meanweight ppgl1 ppglp5 dropclean procorg prodorg maxpricepergramwh* meanmaxweight pricepergramwh pricepergramwh_wgt logpricewh logpricewh_wgt  potency_thctotal potency_cbd  childweekly,  by(weekly salesloc procloc strains vertical inventorytype) fast
*inventoryid 
 
drop if weekly==.
drop if procloc==.

 cd "${dataclean}"
save collapsed_wholesale_clean_forfirstdiffincidence.dta, replace  


*now create final data set for anal:
set more off
cd "${dataclean}"
use  collapsed_wholesale_clean_forfirstdiffincidence.dta, clear

keep if inventorytype==1
gen timetoProcessor=weekly-childweekly

gen TaxChange=0
replace TaxChange=1 if weekly>=2886
gen run=weekly-2886
gen runtax=run*TaxChange

gen Placebo=weekly>=2886+52
gen prun=weekly-(2886+52)
gen prunplacebo=prun*Placebo

egen firstsaledate=min(weekly), by(procloc)
egen lastsaledate=max(weekly), by(procloc)

egen firstprocsaledate=min(weekly), by(procloc salesloc)
gen procretailrelation=weekly-firstprocsaledate

egen firstretailsaledate=min(weekly), by(salesloc)
keep if firstsaledate<2886-7&lastsaledate>=2886+7
*keep if abs(run)<=24
gen week1=weekly==2886

	gen tmpuw=usableweight if usableweightwh>0&usableweightwh<.
	egen countsales=count(tmpuw), by(procloc)
	drop tmpuw
	tab countsales
	tab procloc if countsales<14
	drop if countsales<=14
	
		gen tmpuw=usableweight if usableweightwh>0&usableweightwh<.&run>=-9&run<0
		egen tag=tag(procloc weekly)  if tmpuw<. 
		replace tag=0 if tag==.
	egen tmpuw2=total(tag) if run>=-9&run<0, by(procloc)
	egen countweeks=max(tmpuw2), by(procloc)
	drop tmpuw* tag
	tab countweeks
	tab procloc if countweeks<=4

/*this test overwhelmingly supports that processor prices are always listed tax-exclusive (i.e. price paid by retailer to purchase cannabis)
*test for tax inclusive prices pre-reform:
gen floorprice25=floor(price25)
gen floorprice=floor(price)
gen tmpx=abs(price25-floorprice25-.33)<=.01&TaxChange==0
gen tmpx2=abs(price25-floorprice25-.66)<=.01&TaxChange==0
replace tmpx=tmpx+tmpx2
egen mean33pre=mean(tmpx),by(procloc)
drop tmpx*
gen tmpx=abs(price-floorprice-.33)<=.01&TaxChange==1
gen tmpx2=abs(price-floorprice-.66)<=.01&TaxChange==1
replace tmpx=tmpx+tmpx2
egen mean33post=mean(tmpx),by(procloc)
tab procloc if mean33post-mean33pre>=0
summ mean33*

*alternatively, let's check our default that they are always tax exclusive (price actually paid by retailer)
drop tmpx* mean33*
gen tmpx=abs(price-floorprice-.33)<=.01&TaxChange==0
gen tmpx2=abs(price-floorprice-.66)<=.01&TaxChange==0
replace tmpx=tmpx+tmpx2
egen mean33pre=mean(tmpx),by(procloc)
drop tmpx*
gen tmpx=abs(price-floorprice-.33)<=.01&TaxChange==1
gen tmpx2=abs(price-floorprice-.66)<=.01&TaxChange==1
replace tmpx=tmpx+tmpx2
egen mean33post=mean(tmpx),by(procloc)
summ mean33*
*/

egen group_s=group(strains)

*mixed strain is a very small share of total, so drop them because "mixed" is not a very helpful fixed effect.
count if (strains=="mixed"|strains=="unknown")&abs(run)<=8 
count if abs(run)<=8 
drop if strains=="mixed"|strains=="unknown" 

mmerge salesloc weekly using  "shares_comps_10mile_weekly.dta", type(n:1) unmatched(master) ukeep(mkt_sales num_comp share) umatch(source_loc week)

*note: this is made in retail do files - make_incidenceanal.do
mmerge procloc salesloc  weekly using procretailshares.dta, type(n:1) unmatched(master) umatch(procloc location  weekly)  ukeep(procretailshare  )
 
gen tmpx=procretailshare if weekly>=2886-8&weekly<2886
egen procretailsharepre=mean(tmpx), by(procloc salesloc)
drop tmpx

gen tmpx=procretailshare if weekly>=2886-8+52&weekly<2886+52
egen procretailsharepl=mean(tmpx), by(procloc salesloc)
drop tmpx

*leave-one-out:
set more off
egen tmpx=group(procorg) if weekly>=2886-8&weekly<2886
egen group_p=max(tmpx), by(procorg)
drop tmpx
gen pricestrainweekly_lv1o=.
summ group_p
local rmax = r(max)
forvalues i=1(1)`rmax'{
di `i'
qui gen tmpx=pricepergramwh*usableweightwh if group_p!=`i'
qui gen tmpxx=usableweightwh if group_p!=`i'
qui egen tmpx2=total(tmpx), by(group_s weekly)
qui egen tmpx3=total(tmpxx), by(group_s weekly)
qui replace pricestrainweekly_lv1o=tmpx2/tmpx3 if group_p==`i'
qui drop tmpx*
}
gen logpricedev_lv1o=log(pricepergramwh)-log(pricestrainweekly_lv1o)

 gen tmpx=logpricedev_lv1o if weekly>=2886-8&weekly<2886
egen logpricedevprocpre_lv1os=mean(tmpx), by(procloc group_s)
 drop tmpx
 
   gen tmpx=logpricedev_lv1o if weekly>=2886-8+52&weekly<2886+52
egen logpricedevprocpl_lv1os=mean(tmpx), by(procloc group_s)
 drop tmpx 
 
  gen roundprice=round(pricepergramwh)
 gen priceroundwhole= roundprice==pricepergramwh
 

***SET UP FIRST DIFF ANAL: 
* egen firstparentlotdate=min(weekly), by(parentlot)
egen groupx=group(salesloc procloc strains vertical )
*parentlot 
drop if groupx==.
xtset groupx weekly
gen logmaxprice=log(maxpricepergramwh)

gen pricechange=0
replace pricechange=1 if abs(maxpricepergramwh-L.maxpricepergramwh)>.1&maxpricepergramwh<.&L.maxpricepergramwh<.
replace pricechange=1 if abs(maxpricepergramwh-L2.maxpricepergramwh)>.1&maxpricepergramwh<.&L2.maxpricepergramwh<.&L.maxpricepergramwh==.
replace pricechange=1 if abs(maxpricepergramwh-L3.maxpricepergramwh)>.1&maxpricepergramwh<.&L3.maxpricepergramwh<.&L.maxpricepergramwh==.&L2.maxpricepergramwh==.
replace pricechange=1 if abs(maxpricepergramwh-L4.maxpricepergramwh)>.1&maxpricepergramwh<.&L4.maxpricepergramwh<.&L.maxpricepergramwh==.&L2.maxpricepergramwh==.&L3.maxpricepergramwh==.
gen pricechange2=pricechange
replace pricechange2=1 if abs(maxpricepergramwh-L5.maxpricepergramwh)>.1&maxpricepergramwh<.&L5.maxpricepergramwh<.&L.maxpricepergramwh==.&L2.maxpricepergramwh==.&L3.maxpricepergramwh==.&L4.maxpricepergramwh==.
replace pricechange2=1 if abs(maxpricepergramwh-L6.maxpricepergramwh)>.1&maxpricepergramwh<.&L6.maxpricepergramwh<.&L.maxpricepergramwh==.&L2.maxpricepergramwh==.&L3.maxpricepergramwh==.&L4.maxpricepergramwh==.&L5.maxpricepergramwh==.
replace pricechange2=1 if abs(maxpricepergramwh-L7.maxpricepergramwh)>.1&maxpricepergramwh<.&L7.maxpricepergramwh<.&L.maxpricepergramwh==.&L2.maxpricepergramwh==.&L3.maxpricepergramwh==.&L4.maxpricepergramwh==.&L5.maxpricepergramwh==.&L6.maxpricepergramwh==.
replace pricechange2=1 if abs(maxpricepergramwh-L8.maxpricepergramwh)>.1&maxpricepergramwh<.&L8.maxpricepergramwh<.&L.maxpricepergramwh==.&L2.maxpricepergramwh==.&L3.maxpricepergramwh==.&L4.maxpricepergramwh==.&L5.maxpricepergramwh==.&L6.maxpricepergramwh==.&L7.maxpricepergramwh==.
replace pricechange2=. if maxpricepergramwh<.&L8.maxpricepergramwh==.&L.maxpricepergramwh==.&L2.maxpricepergramwh==.&L3.maxpricepergramwh==.&L4.maxpricepergramwh==.&L5.maxpricepergramwh==.&L6.maxpricepergramwh==.&L7.maxpricepergramwh==.
replace pricechange=. if maxpricepergramwh<.&L4.maxpricepergramwh==.&L.maxpricepergramwh==.&L2.maxpricepergramwh==.&L3.maxpricepergramwh==.

egen totpricechange2=total(pricechange2) if abs(run)<=8, by(groupx)

gen taxdiff=0
replace taxdiff=TaxChange-L.TaxChange if TaxChange<.&L.TaxChange<.
replace taxdiff=TaxChange-L2.TaxChange if TaxChange<.&L2.TaxChange<.&L.TaxChange==.
replace taxdiff=TaxChange-L3.TaxChange if TaxChange<.&L3.TaxChange<.&L.TaxChange==.&L2.TaxChange==.
replace taxdiff=TaxChange-L4.TaxChange if L4.TaxChange<.&L.TaxChange==.&L2.TaxChange==.&L3.TaxChange==.
gen taxdiff2=taxdiff
replace taxdiff2=TaxChange-L5.TaxChange if TaxChange<.&L5.TaxChange<.&L.TaxChange==.&L2.TaxChange==.&L3.TaxChange==.&L4.TaxChange==.
replace taxdiff2=TaxChange-L6.TaxChange if TaxChange<.&L6.TaxChange<.&L.TaxChange==.&L2.TaxChange==.&L3.TaxChange==.&L4.TaxChange==.&L5.TaxChange==.
replace taxdiff2=TaxChange-L7.TaxChange if TaxChange<.&L7.TaxChange<.&L.TaxChange==.&L2.TaxChange==.&L3.TaxChange==.&L4.TaxChange==.&L5.TaxChange==.&L6.TaxChange==.
replace taxdiff2=TaxChange-L8.TaxChange if TaxChange<.&L8.TaxChange<.&L.TaxChange==.&L2.TaxChange==.&L3.TaxChange==.&L4.TaxChange==.&L5.TaxChange==.&L6.TaxChange==.&L7.TaxChange==.
replace taxdiff2=. if TaxChange<.&L8.TaxChange==.&L.TaxChange==.&L2.TaxChange==.&L3.TaxChange==.&L4.TaxChange==.&L5.TaxChange==.&L6.TaxChange==.&L7.TaxChange==.
replace taxdiff=. if L4.TaxChange==.&L.TaxChange==.&L2.TaxChange==.&L3.TaxChange==.

gen FTaxChange=weekly>=2887
gen Ftaxdiff=0
replace Ftaxdiff=FTaxChange-L.FTaxChange if FTaxChange<.&L.FTaxChange<.
replace Ftaxdiff=FTaxChange-L2.FTaxChange if FTaxChange<.&L2.FTaxChange<.&L.FTaxChange==.
replace Ftaxdiff=FTaxChange-L3.FTaxChange if FTaxChange<.&L3.FTaxChange<.&L.FTaxChange==.&L2.FTaxChange==.
replace Ftaxdiff=FTaxChange-L4.FTaxChange if L4.FTaxChange<.&L.FTaxChange==.&L2.FTaxChange==.&L3.FTaxChange==.
gen Ftaxdiff2=Ftaxdiff
replace Ftaxdiff2=FTaxChange-L5.FTaxChange if FTaxChange<.&L5.FTaxChange<.&L.FTaxChange==.&L2.FTaxChange==.&L3.FTaxChange==.&L4.FTaxChange==.
replace Ftaxdiff2=FTaxChange-L6.FTaxChange if FTaxChange<.&L6.FTaxChange<.&L.FTaxChange==.&L2.FTaxChange==.&L3.FTaxChange==.&L4.FTaxChange==.&L5.FTaxChange==.
replace Ftaxdiff2=FTaxChange-L7.FTaxChange if FTaxChange<.&L7.FTaxChange<.&L.FTaxChange==.&L2.FTaxChange==.&L3.FTaxChange==.&L4.FTaxChange==.&L5.FTaxChange==.&L6.FTaxChange==.
replace Ftaxdiff2=FTaxChange-L8.FTaxChange if FTaxChange<.&L8.FTaxChange<.&L.FTaxChange==.&L2.FTaxChange==.&L3.FTaxChange==.&L4.FTaxChange==.&L5.FTaxChange==.&L6.FTaxChange==.&L7.FTaxChange==.
replace Ftaxdiff2=. if FTaxChange<.&L8.FTaxChange==.&L.FTaxChange==.&L2.FTaxChange==.&L3.FTaxChange==.&L4.FTaxChange==.&L5.FTaxChange==.&L6.FTaxChange==.&L7.FTaxChange==.
replace Ftaxdiff=. if L4.FTaxChange==.&L.FTaxChange==.&L2.FTaxChange==.&L3.FTaxChange==.

gen F2TaxChange=weekly>=2888
gen F2taxdiff=0
replace F2taxdiff=F2TaxChange-L.F2TaxChange if F2TaxChange<.&L.F2TaxChange<.
replace F2taxdiff=F2TaxChange-L2.F2TaxChange if F2TaxChange<.&L2.F2TaxChange<.&L.F2TaxChange==.
replace F2taxdiff=F2TaxChange-L3.F2TaxChange if F2TaxChange<.&L3.F2TaxChange<.&L.F2TaxChange==.&L2.F2TaxChange==.
replace F2taxdiff=F2TaxChange-L4.F2TaxChange if L4.F2TaxChange<.&L.F2TaxChange==.&L2.F2TaxChange==.&L3.F2TaxChange==.
gen F2taxdiff2=F2taxdiff
replace F2taxdiff2=F2TaxChange-L5.F2TaxChange if F2TaxChange<.&L5.F2TaxChange<.&L.F2TaxChange==.&L2.F2TaxChange==.&L3.F2TaxChange==.&L4.F2TaxChange==.
replace F2taxdiff2=F2TaxChange-L6.F2TaxChange if F2TaxChange<.&L6.F2TaxChange<.&L.F2TaxChange==.&L2.F2TaxChange==.&L3.F2TaxChange==.&L4.F2TaxChange==.&L5.F2TaxChange==.
replace F2taxdiff2=F2TaxChange-L7.F2TaxChange if F2TaxChange<.&L7.F2TaxChange<.&L.F2TaxChange==.&L2.F2TaxChange==.&L3.F2TaxChange==.&L4.F2TaxChange==.&L5.F2TaxChange==.&L6.F2TaxChange==.
replace F2taxdiff2=F2TaxChange-L8.F2TaxChange if F2TaxChange<.&L8.F2TaxChange<.&L.F2TaxChange==.&L2.F2TaxChange==.&L3.F2TaxChange==.&L4.F2TaxChange==.&L5.F2TaxChange==.&L6.F2TaxChange==.&L7.F2TaxChange==.
replace F2taxdiff2=. if F2TaxChange<.&L8.F2TaxChange==.&L.F2TaxChange==.&L2.F2TaxChange==.&L3.F2TaxChange==.&L4.F2TaxChange==.&L5.F2TaxChange==.&L6.F2TaxChange==.&L7.F2TaxChange==.
replace F2taxdiff=. if L4.F2TaxChange==.&L.F2TaxChange==.&L2.F2TaxChange==.&L3.F2TaxChange==.


gen F3TaxChange=weekly>=2889
gen F3taxdiff=0
replace F3taxdiff=F3TaxChange-L.F3TaxChange if F3TaxChange<.&L.F3TaxChange<.
replace F3taxdiff=F3TaxChange-L2.F3TaxChange if F3TaxChange<.&L2.F3TaxChange<.&L.F3TaxChange==.
replace F3taxdiff=F3TaxChange-L3.F3TaxChange if F3TaxChange<.&L3.F3TaxChange<.&L.F3TaxChange==.&L2.F3TaxChange==.
replace F3taxdiff=F3TaxChange-L4.F3TaxChange if L4.F3TaxChange<.&L.F3TaxChange==.&L2.F3TaxChange==.&L3.F3TaxChange==.
gen F3taxdiff2=F3taxdiff
replace F3taxdiff2=F3TaxChange-L5.F3TaxChange if F3TaxChange<.&L5.F3TaxChange<.&L.F3TaxChange==.&L2.F3TaxChange==.&L3.F3TaxChange==.&L4.F3TaxChange==.
replace F3taxdiff2=F3TaxChange-L6.F3TaxChange if F3TaxChange<.&L6.F3TaxChange<.&L.F3TaxChange==.&L2.F3TaxChange==.&L3.F3TaxChange==.&L4.F3TaxChange==.&L5.F3TaxChange==.
replace F3taxdiff2=F3TaxChange-L7.F3TaxChange if F3TaxChange<.&L7.F3TaxChange<.&L.F3TaxChange==.&L2.F3TaxChange==.&L3.F3TaxChange==.&L4.F3TaxChange==.&L5.F3TaxChange==.&L6.F3TaxChange==.
replace F3taxdiff2=F3TaxChange-L8.F3TaxChange if F3TaxChange<.&L8.F3TaxChange<.&L.F3TaxChange==.&L2.F3TaxChange==.&L3.F3TaxChange==.&L4.F3TaxChange==.&L5.F3TaxChange==.&L6.F3TaxChange==.&L7.F3TaxChange==.
replace F3taxdiff2=. if F3TaxChange<.&L8.F3TaxChange==.&L.F3TaxChange==.&L2.F3TaxChange==.&L3.F3TaxChange==.&L4.F3TaxChange==.&L5.F3TaxChange==.&L6.F3TaxChange==.&L7.F3TaxChange==.
replace F3taxdiff=. if L4.F3TaxChange==.&L.F3TaxChange==.&L2.F3TaxChange==.&L3.F3TaxChange==.

 gen LTaxChange=weekly>=2885
gen LTaxdiff=0
replace LTaxdiff=LTaxChange-L.LTaxChange if LTaxChange<.&L.LTaxChange<.
replace LTaxdiff=LTaxChange-L2.LTaxChange if LTaxChange<.&L2.LTaxChange<.&L.LTaxChange==.
replace LTaxdiff=LTaxChange-L3.LTaxChange if LTaxChange<.&L3.LTaxChange<.&L.LTaxChange==.&L2.LTaxChange==.
replace LTaxdiff=LTaxChange-L4.LTaxChange if L4.LTaxChange<.&L.LTaxChange==.&L2.LTaxChange==.&L3.LTaxChange==.
gen Ltaxdiff2=LTaxdiff
replace Ltaxdiff2=LTaxChange-L5.LTaxChange if LTaxChange<.&L5.LTaxChange<.&L.LTaxChange==.&L2.LTaxChange==.&L3.LTaxChange==.&L4.LTaxChange==.
replace Ltaxdiff2=LTaxChange-L6.LTaxChange if LTaxChange<.&L6.LTaxChange<.&L.LTaxChange==.&L2.LTaxChange==.&L3.LTaxChange==.&L4.LTaxChange==.&L5.LTaxChange==.
replace Ltaxdiff2=LTaxChange-L7.LTaxChange if LTaxChange<.&L7.LTaxChange<.&L.LTaxChange==.&L2.LTaxChange==.&L3.LTaxChange==.&L4.LTaxChange==.&L5.LTaxChange==.&L6.LTaxChange==.
replace Ltaxdiff2=LTaxChange-L8.LTaxChange if LTaxChange<.&L8.LTaxChange<.&L.LTaxChange==.&L2.LTaxChange==.&L3.LTaxChange==.&L4.LTaxChange==.&L5.LTaxChange==.&L6.LTaxChange==.&L7.LTaxChange==.
replace Ltaxdiff2=. if LTaxChange<.&L8.LTaxChange==.&L.LTaxChange==.&L2.LTaxChange==.&L3.LTaxChange==.&L4.LTaxChange==.&L5.LTaxChange==.&L6.LTaxChange==.&L7.LTaxChange==.
replace LTaxdiff=. if L4.LTaxChange==.&L.LTaxChange==.&L2.LTaxChange==.&L3.LTaxChange==.

 gen L2TaxChange=weekly>=2884
gen L2Taxdiff=0
replace L2Taxdiff=L2TaxChange-L.L2TaxChange if L2TaxChange<.&L.L2TaxChange<.
replace L2Taxdiff=L2TaxChange-L2.L2TaxChange if L2TaxChange<.&L2.L2TaxChange<.&L.L2TaxChange==.
replace L2Taxdiff=L2TaxChange-L3.L2TaxChange if L2TaxChange<.&L3.L2TaxChange<.&L.L2TaxChange==.&L2.L2TaxChange==.
replace L2Taxdiff=L2TaxChange-L4.L2TaxChange if L4.L2TaxChange<.&L.L2TaxChange==.&L2.L2TaxChange==.&L3.L2TaxChange==.
gen L2taxdiff2=L2Taxdiff
replace L2taxdiff2=L2TaxChange-L5.L2TaxChange if L2TaxChange<.&L5.L2TaxChange<.&L.L2TaxChange==.&L2.L2TaxChange==.&L3.L2TaxChange==.&L4.L2TaxChange==.
replace L2taxdiff2=L2TaxChange-L6.L2TaxChange if L2TaxChange<.&L6.L2TaxChange<.&L.L2TaxChange==.&L2.L2TaxChange==.&L3.L2TaxChange==.&L4.L2TaxChange==.&L5.L2TaxChange==.
replace L2taxdiff2=L2TaxChange-L7.L2TaxChange if L2TaxChange<.&L7.L2TaxChange<.&L.L2TaxChange==.&L2.L2TaxChange==.&L3.L2TaxChange==.&L4.L2TaxChange==.&L5.L2TaxChange==.&L6.L2TaxChange==.
replace L2taxdiff2=L2TaxChange-L8.L2TaxChange if L2TaxChange<.&L8.L2TaxChange<.&L.L2TaxChange==.&L2.L2TaxChange==.&L3.L2TaxChange==.&L4.L2TaxChange==.&L5.L2TaxChange==.&L6.L2TaxChange==.&L7.L2TaxChange==.
replace L2taxdiff2=. if L2TaxChange<.&L8.L2TaxChange==.&L.L2TaxChange==.&L2.L2TaxChange==.&L3.L2TaxChange==.&L4.L2TaxChange==.&L5.L2TaxChange==.&L6.L2TaxChange==.&L7.L2TaxChange==.
replace L2Taxdiff=. if L4.L2TaxChange==.&L.L2TaxChange==.&L2.L2TaxChange==.&L3.L2TaxChange==.

 gen L3TaxChange=weekly>=2883
gen L3Taxdiff=0
replace L3Taxdiff=L3TaxChange-L.L3TaxChange if L3TaxChange<.&L.L3TaxChange<.
replace L3Taxdiff=L3TaxChange-L3.L3TaxChange if L3TaxChange<.&L3.L3TaxChange<.&L.L3TaxChange==.
replace L3Taxdiff=L3TaxChange-L3.L3TaxChange if L3TaxChange<.&L3.L3TaxChange<.&L.L3TaxChange==.&L3.L3TaxChange==.
replace L3Taxdiff=L3TaxChange-L4.L3TaxChange if L4.L3TaxChange<.&L.L3TaxChange==.&L3.L3TaxChange==.&L3.L3TaxChange==.
gen L3taxdiff2=L3Taxdiff
replace L3taxdiff2=L3TaxChange-L5.L3TaxChange if L3TaxChange<.&L5.L3TaxChange<.&L.L3TaxChange==.&L3.L3TaxChange==.&L3.L3TaxChange==.&L4.L3TaxChange==.
replace L3taxdiff2=L3TaxChange-L6.L3TaxChange if L3TaxChange<.&L6.L3TaxChange<.&L.L3TaxChange==.&L3.L3TaxChange==.&L3.L3TaxChange==.&L4.L3TaxChange==.&L5.L3TaxChange==.
replace L3taxdiff2=L3TaxChange-L7.L3TaxChange if L3TaxChange<.&L7.L3TaxChange<.&L.L3TaxChange==.&L3.L3TaxChange==.&L3.L3TaxChange==.&L4.L3TaxChange==.&L5.L3TaxChange==.&L6.L3TaxChange==.
replace L3taxdiff2=L3TaxChange-L8.L3TaxChange if L3TaxChange<.&L8.L3TaxChange<.&L.L3TaxChange==.&L3.L3TaxChange==.&L3.L3TaxChange==.&L4.L3TaxChange==.&L5.L3TaxChange==.&L6.L3TaxChange==.&L7.L3TaxChange==.
replace L3taxdiff2=. if L3TaxChange<.&L8.L3TaxChange==.&L.L3TaxChange==.&L3.L3TaxChange==.&L3.L3TaxChange==.&L4.L3TaxChange==.&L5.L3TaxChange==.&L6.L3TaxChange==.&L7.L3TaxChange==.
replace L3Taxdiff=. if L4.L3TaxChange==.&L.L3TaxChange==.&L3.L3TaxChange==.&L3.L3TaxChange==.

 
gen difflength=0
replace difflength=1 if maxpricepergramwh!=L.maxpricepergramwh&maxpricepergramwh<.&L.maxpricepergramwh<.
replace difflength=2 if maxpricepergramwh!=L2.maxpricepergramwh&maxpricepergramwh<.&L2.maxpricepergramwh<.&L.maxpricepergramwh==.
replace difflength=3 if maxpricepergramwh!=L3.maxpricepergramwh&maxpricepergramwh<.&L3.maxpricepergramwh<.&L.maxpricepergramwh==.&L2.maxpricepergramwh==.
replace difflength=4 if maxpricepergramwh!=L4.maxpricepergramwh&maxpricepergramwh<.&L4.maxpricepergramwh<.&L.maxpricepergramwh==.&L2.maxpricepergramwh==.&L3.maxpricepergramwh==.
gen difflength2=difflength
replace difflength2=5 if maxpricepergramwh!=L5.maxpricepergramwh&maxpricepergramwh<.&L5.maxpricepergramwh<.&L.maxpricepergramwh==.&L2.maxpricepergramwh==.&L3.maxpricepergramwh==.&L4.maxpricepergramwh==.
replace difflength2=6 if maxpricepergramwh!=L6.maxpricepergramwh&maxpricepergramwh<.&L6.maxpricepergramwh<.&L.maxpricepergramwh==.&L2.maxpricepergramwh==.&L3.maxpricepergramwh==.&L4.maxpricepergramwh==.&L5.maxpricepergramwh==.
replace difflength2=7 if maxpricepergramwh!=L7.maxpricepergramwh&maxpricepergramwh<.&L7.maxpricepergramwh<.&L.maxpricepergramwh==.&L2.maxpricepergramwh==.&L3.maxpricepergramwh==.&L4.maxpricepergramwh==.&L5.maxpricepergramwh==.&L6.maxpricepergramwh==.
replace difflength2=8 if maxpricepergramwh!=L8.maxpricepergramwh&maxpricepergramwh<.&L8.maxpricepergramwh<.&L.maxpricepergramwh==.&L2.maxpricepergramwh==.&L3.maxpricepergramwh==.&L4.maxpricepergramwh==.&L5.maxpricepergramwh==.&L6.maxpricepergramwh==.&L7.maxpricepergramwh==.
replace difflength2=. if maxpricepergramwh<.&L8.maxpricepergramwh==.&L.maxpricepergramwh==.&L2.maxpricepergramwh==.&L3.maxpricepergramwh==.&L4.maxpricepergramwh==.&L5.maxpricepergramwh==.&L6.maxpricepergramwh==.&L7.maxpricepergramwh==.

  gen placebodiff=0
replace placebodiff=Placebo-L.Placebo if Placebo<.&L.Placebo<.
replace placebodiff=Placebo-L2.Placebo if Placebo<.&L2.Placebo<.&L.Placebo==.
replace placebodiff=Placebo-L3.Placebo if Placebo<.&L3.Placebo<.&L.Placebo==.&L2.Placebo==.
replace placebodiff=Placebo-L4.Placebo if Placebo<.&L4.Placebo<.&L.Placebo==.&L2.Placebo==.&L3.Placebo==.
gen placebodiff2=placebodiff
replace placebodiff2=Placebo-L5.Placebo if Placebo<.&L5.Placebo<.&L.Placebo==.&L2.Placebo==.&L3.Placebo==.&L4.Placebo==.
replace placebodiff2=Placebo-L6.Placebo if Placebo<.&L6.Placebo<.&L.Placebo==.&L2.Placebo==.&L3.Placebo==.&L4.Placebo==.&L5.Placebo==.
replace placebodiff2=Placebo-L7.Placebo if Placebo<.&L7.Placebo<.&L.Placebo==.&L2.Placebo==.&L3.Placebo==.&L4.Placebo==.&L5.Placebo==.&L6.Placebo==.
replace placebodiff2=Placebo-L8.Placebo if Placebo<.&L8.Placebo<.&L.Placebo==.&L2.Placebo==.&L3.Placebo==.&L4.Placebo==.&L5.Placebo==.&L6.Placebo==.&L7.Placebo==.
replace placebodiff2=Placebo-L8.Placebo if Placebo<.&L8.Placebo<.&L.Placebo==.&L2.Placebo==.&L3.Placebo==.&L4.Placebo==.&L5.Placebo==.&L6.Placebo==.&L7.Placebo==.
replace placebodiff2=. if Placebo<.&L8.Placebo==.&L.Placebo==.&L2.Placebo==.&L3.Placebo==.&L4.Placebo==.&L5.Placebo==.&L6.Placebo==.&L7.Placebo==.
replace placebodiff=. if Placebo<.&L4.Placebo==.&L.Placebo==.&L2.Placebo==.&L3.Placebo==.
 
 
 gen pricediff3=0
replace pricediff3=pricepergramwh-L.pricepergramwh  
replace pricediff3=pricepergramwh-L2.pricepergramwh if  maxpricepergramwh<.&L2.maxpricepergramwh<.&L.maxpricepergramwh==.
replace pricediff3=pricepergramwh-L3.pricepergramwh if  maxpricepergramwh<.&L3.maxpricepergramwh<.&L.maxpricepergramwh==.&L2.maxpricepergramwh==.
replace pricediff3=pricepergramwh-L4.pricepergramwh if maxpricepergramwh<.&L4.maxpricepergramwh<.&L.maxpricepergramwh==.&L2.maxpricepergramwh==.&L3.maxpricepergramwh==.
gen pricediff4=pricediff3
replace pricediff4=pricepergramwh-L5.pricepergramwh if maxpricepergramwh<.&L5.maxpricepergramwh<.&L.maxpricepergramwh==.&L2.maxpricepergramwh==.&L3.maxpricepergramwh==.&L4.maxpricepergramwh==.
replace pricediff4=pricepergramwh-L6.pricepergramwh if  maxpricepergramwh<.&L6.maxpricepergramwh<.&L.maxpricepergramwh==.&L2.maxpricepergramwh==.&L3.maxpricepergramwh==.&L4.maxpricepergramwh==.&L5.maxpricepergramwh==.
replace pricediff4=pricepergramwh-L7.pricepergramwh if maxpricepergramwh<.&L7.maxpricepergramwh<.&L.maxpricepergramwh==.&L2.maxpricepergramwh==.&L3.maxpricepergramwh==.&L4.maxpricepergramwh==.&L5.maxpricepergramwh==.&L6.maxpricepergramwh==.
replace pricediff4=pricepergramwh-L8.pricepergramwh if  maxpricepergramwh<.&L8.maxpricepergramwh<.&L.maxpricepergramwh==.&L2.maxpricepergramwh==.&L3.maxpricepergramwh==.&L4.maxpricepergramwh==.&L5.maxpricepergramwh==.&L6.maxpricepergramwh==.&L7.maxpricepergramwh==.
replace pricediff4=. if  maxpricepergramwh<.&L8.maxpricepergramwh==.&L.maxpricepergramwh==.&L2.maxpricepergramwh==.&L3.maxpricepergramwh==.&L4.maxpricepergramwh==.&L5.maxpricepergramwh==.&L6.maxpricepergramwh==.&L7.maxpricepergramwh==.
 replace pricediff3=. if L4.maxpricepergramwh==.&L.maxpricepergramwh==.&L2.maxpricepergramwh==.&L3.maxpricepergramwh==.

 gen pricediff6=0
replace pricediff6=logpricewh_wgt-L.logpricewh_wgt  
replace pricediff6=logpricewh_wgt-L2.logpricewh_wgt if  maxpricepergramwh<.&L2.maxpricepergramwh<.&L.maxpricepergramwh==.
replace pricediff6=logpricewh_wgt-L3.logpricewh_wgt if  maxpricepergramwh<.&L3.maxpricepergramwh<.&L.maxpricepergramwh==.&L2.maxpricepergramwh==.
replace pricediff6=logpricewh_wgt-L4.logpricewh_wgt if maxpricepergramwh<.&L4.maxpricepergramwh<.&L.maxpricepergramwh==.&L2.maxpricepergramwh==.&L3.maxpricepergramwh==.
replace pricediff6=logpricewh_wgt-L5.logpricewh_wgt if maxpricepergramwh<.&L5.maxpricepergramwh<.&L.maxpricepergramwh==.&L2.maxpricepergramwh==.&L3.maxpricepergramwh==.&L4.maxpricepergramwh==.
replace pricediff6=logpricewh_wgt-L6.logpricewh_wgt if  maxpricepergramwh<.&L6.maxpricepergramwh<.&L.maxpricepergramwh==.&L2.maxpricepergramwh==.&L3.maxpricepergramwh==.&L4.maxpricepergramwh==.&L5.maxpricepergramwh==.
replace pricediff6=logpricewh_wgt-L7.logpricewh_wgt if maxpricepergramwh<.&L7.maxpricepergramwh<.&L.maxpricepergramwh==.&L2.maxpricepergramwh==.&L3.maxpricepergramwh==.&L4.maxpricepergramwh==.&L5.maxpricepergramwh==.&L6.maxpricepergramwh==.
replace pricediff6=logpricewh_wgt-L8.logpricewh_wgt if  maxpricepergramwh<.&L8.maxpricepergramwh<.&L.maxpricepergramwh==.&L2.maxpricepergramwh==.&L3.maxpricepergramwh==.&L4.maxpricepergramwh==.&L5.maxpricepergramwh==.&L6.maxpricepergramwh==.&L7.maxpricepergramwh==.
replace pricediff6=. if  maxpricepergramwh<.&L8.maxpricepergramwh==.&L.maxpricepergramwh==.&L2.maxpricepergramwh==.&L3.maxpricepergramwh==.&L4.maxpricepergramwh==.&L5.maxpricepergramwh==.&L6.maxpricepergramwh==.&L7.maxpricepergramwh==.

  gen weight5=0
replace weight5 =usableweight+L.usableweightwh  
replace weight5 =usableweight+L2.usableweightwh if  maxpricepergramwh<.&L2.maxpricepergramwh<.&L.maxpricepergramwh==.
replace weight5 =usableweight+L3.usableweightwh if  maxpricepergramwh<.&L3.maxpricepergramwh<.&L.maxpricepergramwh==.&L2.maxpricepergramwh==.
replace weight5 =usableweight+L4.usableweightwh if maxpricepergramwh<.&L4.maxpricepergramwh<.&L.maxpricepergramwh==.&L2.maxpricepergramwh==.&L3.maxpricepergramwh==.
gen weight6=weight5
replace weight6 =usableweight+L5.usableweightwh if maxpricepergramwh<.&L5.maxpricepergramwh<.&L.maxpricepergramwh==.&L2.maxpricepergramwh==.&L3.maxpricepergramwh==.&L4.maxpricepergramwh==.
replace weight6 =usableweight+L6.usableweightwh if  maxpricepergramwh<.&L6.maxpricepergramwh<.&L.maxpricepergramwh==.&L2.maxpricepergramwh==.&L3.maxpricepergramwh==.&L4.maxpricepergramwh==.&L5.maxpricepergramwh==.
replace weight6 =usableweight+L7.usableweightwh if maxpricepergramwh<.&L7.maxpricepergramwh<.&L.maxpricepergramwh==.&L2.maxpricepergramwh==.&L3.maxpricepergramwh==.&L4.maxpricepergramwh==.&L5.maxpricepergramwh==.&L6.maxpricepergramwh==.
replace weight6 =usableweight+L8.usableweightwh if  maxpricepergramwh<.&L8.maxpricepergramwh<.&L.maxpricepergramwh==.&L2.maxpricepergramwh==.&L3.maxpricepergramwh==.&L4.maxpricepergramwh==.&L5.maxpricepergramwh==.&L6.maxpricepergramwh==.&L7.maxpricepergramwh==.
replace weight6=. if  maxpricepergramwh<.&L8.maxpricepergramwh==.&L.maxpricepergramwh==.&L2.maxpricepergramwh==.&L3.maxpricepergramwh==.&L4.maxpricepergramwh==.&L5.maxpricepergramwh==.&L6.maxpricepergramwh==.&L7.maxpricepergramwh==.
 replace weight5=. if L4.maxpricepergramwh==.&L.maxpricepergramwh==.&L2.maxpricepergramwh==.&L3.maxpricepergramwh==.
 
drop difflength*
gen difflength=0
replace difflength=1 if maxpricepergramwh<.&L.maxpricepergramwh<.
replace difflength=2 if  maxpricepergramwh<.&L2.maxpricepergramwh<.&L.maxpricepergramwh==.
replace difflength=3 if  maxpricepergramwh<.&L3.maxpricepergramwh<.&L.maxpricepergramwh==.&L2.maxpricepergramwh==.
replace difflength=4 if  maxpricepergramwh<.&L4.maxpricepergramwh<.&L.maxpricepergramwh==.&L2.maxpricepergramwh==.&L3.maxpricepergramwh==.
gen difflength2=difflength
replace difflength2=5 if  maxpricepergramwh<.&L5.maxpricepergramwh<.&L.maxpricepergramwh==.&L2.maxpricepergramwh==.&L3.maxpricepergramwh==.&L4.maxpricepergramwh==.
replace difflength2=6 if  maxpricepergramwh<.&L6.maxpricepergramwh<.&L.maxpricepergramwh==.&L2.maxpricepergramwh==.&L3.maxpricepergramwh==.&L4.maxpricepergramwh==.&L5.maxpricepergramwh==.
replace difflength2=7 if  maxpricepergramwh<.&L7.maxpricepergramwh<.&L.maxpricepergramwh==.&L2.maxpricepergramwh==.&L3.maxpricepergramwh==.&L4.maxpricepergramwh==.&L5.maxpricepergramwh==.&L6.maxpricepergramwh==.
replace difflength2=8 if  maxpricepergramwh<.&L8.maxpricepergramwh<.&L.maxpricepergramwh==.&L2.maxpricepergramwh==.&L3.maxpricepergramwh==.&L4.maxpricepergramwh==.&L5.maxpricepergramwh==.&L6.maxpricepergramwh==.&L7.maxpricepergramwh==.
replace difflength2=. if maxpricepergramwh<.&L8.maxpricepergramwh==.&L.maxpricepergramwh==.&L2.maxpricepergramwh==.&L3.maxpricepergramwh==.&L4.maxpricepergramwh==.&L5.maxpricepergramwh==.&L6.maxpricepergramwh==.&L7.maxpricepergramwh==.

gen pricechange3=0
replace pricechange3=1 if abs(maxpricepergramwh-L.maxpricepergramwh)>.01&maxpricepergramwh<.&L.maxpricepergramwh<.
replace pricechange3=1 if abs(maxpricepergramwh-L2.maxpricepergramwh)>.01&maxpricepergramwh<.&L2.maxpricepergramwh<.&L.maxpricepergramwh==.
replace pricechange3=1 if abs(maxpricepergramwh-L3.maxpricepergramwh)>.01&maxpricepergramwh<.&L3.maxpricepergramwh<.&L.maxpricepergramwh==.&L2.maxpricepergramwh==.
replace pricechange3=1 if abs(maxpricepergramwh-L4.maxpricepergramwh)>.01&maxpricepergramwh<.&L4.maxpricepergramwh<.&L.maxpricepergramwh==.&L2.maxpricepergramwh==.&L3.maxpricepergramwh==.
gen pricechange4=pricechange3
replace pricechange4=1 if abs(maxpricepergramwh-L5.maxpricepergramwh)>.01&maxpricepergramwh<.&L5.maxpricepergramwh<.&L.maxpricepergramwh==.&L2.maxpricepergramwh==.&L3.maxpricepergramwh==.&L4.maxpricepergramwh==.
replace pricechange4=1 if abs(maxpricepergramwh-L6.maxpricepergramwh)>.01&maxpricepergramwh<.&L6.maxpricepergramwh<.&L.maxpricepergramwh==.&L2.maxpricepergramwh==.&L3.maxpricepergramwh==.&L4.maxpricepergramwh==.&L5.maxpricepergramwh==.
replace pricechange4=1 if abs(maxpricepergramwh-L7.maxpricepergramwh)>.01&maxpricepergramwh<.&L7.maxpricepergramwh<.&L.maxpricepergramwh==.&L2.maxpricepergramwh==.&L3.maxpricepergramwh==.&L4.maxpricepergramwh==.&L5.maxpricepergramwh==.&L6.maxpricepergramwh==.
replace pricechange4=1 if abs(maxpricepergramwh-L8.maxpricepergramwh)>.01&maxpricepergramwh<.&L8.maxpricepergramwh<.&L.maxpricepergramwh==.&L2.maxpricepergramwh==.&L3.maxpricepergramwh==.&L4.maxpricepergramwh==.&L5.maxpricepergramwh==.&L6.maxpricepergramwh==.&L7.maxpricepergramwh==.
replace pricechange4=. if maxpricepergramwh<.&L8.maxpricepergramwh==.&L.maxpricepergramwh==.&L2.maxpricepergramwh==.&L3.maxpricepergramwh==.&L4.maxpricepergramwh==.&L5.maxpricepergramwh==.&L6.maxpricepergramwh==.&L7.maxpricepergramwh==.
replace pricechange3=. if maxpricepergramwh<.&L4.maxpricepergramwh==.&L.maxpricepergramwh==.&L2.maxpricepergramwh==.&L3.maxpricepergramwh==.

gen taxdiff2_pricechange4=taxdiff2*pricechange4
gen placebodiff2_pricechange4=placebodiff2*pricechange4
gen week420=weekly==2875
gen week420diff=0
replace week420diff=week420-L.week420 if week420<.&L.week420<.
replace week420diff=week420-L2.week420 if week420<.&L2.week420<.&L.week420==.
replace week420diff=week420-L3.week420 if week420<.&L3.week420<.&L.week420==.&L2.week420==.
replace week420diff=week420-L4.week420 if L4.week420<.&L.week420==.&L2.week420==.&L3.week420==.
gen week420diff2=week420diff
replace week420diff2=week420-L5.week420 if week420<.&L5.week420<.&L.week420==.&L2.week420==.&L3.week420==.&L4.week420==.
replace week420diff2=week420-L6.week420 if week420<.&L6.week420<.&L.week420==.&L2.week420==.&L3.week420==.&L4.week420==.&L5.week420==.
replace week420diff2=week420-L7.week420 if week420<.&L7.week420<.&L.week420==.&L2.week420==.&L3.week420==.&L4.week420==.&L5.week420==.&L6.week420==.
replace week420diff2=week420-L8.week420 if week420<.&L8.week420<.&L.week420==.&L2.week420==.&L3.week420==.&L4.week420==.&L5.week420==.&L6.week420==.&L7.week420==.
replace week420diff2=. if week420<.&L8.week420==.&L.week420==.&L2.week420==.&L3.week420==.&L4.week420==.&L5.week420==.&L6.week420==.&L7.week420==.
replace week420diff=. if L4.week420==.&L.week420==.&L2.week420==.&L3.week420==.


gen week420placebo=weekly==2927
gen week420placebodiff=0
replace week420placebodiff=week420placebo-L.week420placebo if week420placebo<.&L.week420placebo<.
replace week420placebodiff=week420placebo-L2.week420placebo if week420placebo<.&L2.week420placebo<.&L.week420placebo==.
replace week420placebodiff=week420placebo-L3.week420placebo if week420placebo<.&L3.week420placebo<.&L.week420placebo==.&L2.week420placebo==.
replace week420placebodiff=week420placebo-L4.week420placebo if L4.week420placebo<.&L.week420placebo==.&L2.week420placebo==.&L3.week420placebo==.
gen week420placebodiff2=week420placebodiff
replace week420placebodiff2=week420placebo-L5.week420placebo if week420placebo<.&L5.week420placebo<.&L.week420placebo==.&L2.week420placebo==.&L3.week420placebo==.&L4.week420placebo==.
replace week420placebodiff2=week420placebo-L6.week420placebo if week420placebo<.&L6.week420placebo<.&L.week420placebo==.&L2.week420placebo==.&L3.week420placebo==.&L4.week420placebo==.&L5.week420placebo==.
replace week420placebodiff2=week420placebo-L7.week420placebo if week420placebo<.&L7.week420placebo<.&L.week420placebo==.&L2.week420placebo==.&L3.week420placebo==.&L4.week420placebo==.&L5.week420placebo==.&L6.week420placebo==.
replace week420placebodiff2=week420placebo-L8.week420placebo if week420placebo<.&L8.week420placebo<.&L.week420placebo==.&L2.week420placebo==.&L3.week420placebo==.&L4.week420placebo==.&L5.week420placebo==.&L6.week420placebo==.&L7.week420placebo==.
replace week420placebodiff2=. if week420placebo<.&L8.week420placebo==.&L.week420placebo==.&L2.week420placebo==.&L3.week420placebo==.&L4.week420placebo==.&L5.week420placebo==.&L6.week420placebo==.&L7.week420placebo==.
replace week420placebodiff=. if L4.week420placebo==.&L.week420placebo==.&L2.week420placebo==.&L3.week420placebo==.

gen F4TaxChange=weekly>=2890
gen L4group=weekly<=2882

gen pricechange5=abs(pricediff6)>.05&abs(pricediff6)<.
replace pricechange5=. if pricediff6==.

gen taxdiff2_pricechange5=taxdiff2*pricechange5

egen firstgroupdate=min(weekly), by(groupx)
gen weekssincefirstgroupdate=weekly-firstgroupdate
egen lastgroupdate=max(weekly), by(groupx)
gen weeksuntillastgroupdate=lastgroupdate-weekly

xtset groupx weekly
gen FPlacebo=weekly>=2887+52
gen Fplacebodiff=FPlacebo-L.FPlacebo
replace Fplacebodiff=FPlacebo-L2.FPlacebo if Fplacebodiff==.
replace Fplacebodiff=FPlacebo-L3.FPlacebo if Fplacebodiff==.
replace Fplacebodiff=FPlacebo-L4.FPlacebo if Fplacebodiff==.

gen F2Placebo=weekly>=2888+52
gen F2placebodiff=F2Placebo-L.F2Placebo
replace F2placebodiff=F2Placebo-L2.F2Placebo if F2placebodiff==.
replace F2placebodiff=F2Placebo-L3.F2Placebo if F2placebodiff==.
replace F2placebodiff=F2Placebo-L4.F2Placebo if F2placebodiff==.

 gen LPlacebo=weekly>=2885+52
gen Lplacebodiff=LPlacebo-L.LPlacebo 
replace Lplacebodiff=LPlacebo-L2.LPlacebo if Lplacebodiff==.
replace Lplacebodiff=LPlacebo-L3.LPlacebo if Lplacebodiff==.
replace Lplacebodiff=LPlacebo-L4.LPlacebo if Lplacebodiff==.

 gen L2Placebo=weekly>=2884+52
gen L2placebodiff=L2Placebo-L.L2Placebo 
replace L2placebodiff=L2Placebo-L2.L2Placebo if L2placebodiff==.
replace L2placebodiff=L2Placebo-L3.L2Placebo if L2placebodiff==.
replace L2placebodiff=L2Placebo-L4.L2Placebo if L2placebodiff==.

 gen L3Placebo=weekly>=2883+52
gen L3placebodiff=L3Placebo-L.L3Placebo  
replace L3placebodiff=L3Placebo-L2.L3Placebo  if L3placebodiff==.
replace L3placebodiff=L3Placebo-L3.L3Placebo  if L3placebodiff==.
replace L3placebodiff=L3Placebo-L4.L3Placebo  if L3placebodiff==.

gen F3Placebo=weekly>=2889+52
gen F3placebodiff=F3Placebo-L.F3Placebo 
replace F3placebodiff=F3Placebo-L2.F3Placebo if F3placebodiff==.
replace F3placebodiff=F3Placebo-L3.F3Placebo if F3placebodiff==.
replace F3placebodiff=F3Placebo-L4.F3Placebo if F3placebodiff==.

gen F4Placebo=weekly>=2890+52
gen F4placebodiff=F4Placebo-L.F4Placebo 
replace F4placebodiff=F4Placebo-L2.F4Placebo if F4placebodiff==.
replace F4placebodiff=F4Placebo-L3.F4Placebo if F4placebodiff==.
replace F4placebodiff=F4Placebo-L4.F4Placebo if F4placebodiff==.

gen L4placebogroup=weekly<=2882+52


  mmerge salesloc procloc strains using meanretailpre_forfirstdiff.dta, umatch(location procloc strain_f) type(n:1) unmatched(master)
 gen centsfortaxinvar=abs(meanretailpre /(.37+ retailsaletaxmultpre)-meanretailpre/(1.25*retailsaletaxmultpre))
 gen centsfortaxinvar2=abs(meanretailfirstweekpre /(.37+ retailsaletaxmultpre)-meanretailfirstweekpre/(1.25*retailsaletaxmultpre))
gen plcentsfortaxinvar=abs(meanretailplacebo /(.37+ retailsaletaxmultpre)-meanretailplacebo/(1.25*retailsaletaxmultpre))
 gen centspct=centsfortaxinvar/meanwholesalepre

cd "${dataclean}"

save  incidence_procanal_firstdiff.dta, replace
 
 




********************************
***CREATE FINAL MONTHLY DATA SET

use full_wholesale_clean_forincidence.dta, clear /*just prior to collapse*/
drop tmpx
egen firstparentlotdate=min(weekly), by(parentlot)
gen tmpx= pricepergramwh if firstparentlotdate==weekly
egen meanpricewhfirst=mean(tmpx), by(parentlot)
drop tmpx
gen tmpx=pricepergramwh if weekly!=firstparentlotdate
egen meanpricewh=mean(tmpx) , by(parentlot)
drop if meanpricewh/meanpricewhfirst>=10& meanpricewh/meanpricewhfirst<.&abs(meanpricewhfirst-pricepergramwh)<.5
gen gap=meanpricewh/meanpricewhfirst>=3&meanpricewh/meanpricewhfirst<10
drop tmpx

gen logpricewh=log(pricepergramwh)

egen tmpx=total(logpricewh*usableweightwh), by(monthly salesloc procloc strains parentlot vertical)
egen tmpx2=total(usableweightwh), by(monthly salesloc procloc strains parentlot vertical)
gen logpricewh_wgt=tmpx/tmpx2

drop tmpx*
egen tmpx=total(pricepergramwh*usableweightwh), by(monthly salesloc procloc strains parentlot vertical)
egen tmpx2=total(usableweightwh), by(monthly salesloc procloc strains parentlot vertical)
gen pricepergramwh_wgt=tmpx/tmpx2

*merge in inventory the day before each sale.
gen datewhm1=datewh-1
cd "$data"
mmerge inventoryid salesloc salesorg datewhm1  using  "inventory_by_date.dta", type(n:1) unmatched(master) umatch(inventoryid salesloc salesorg date) ukeep(weight_at_start_of_day) urename(weight_at_start_of_day invweightdaybefore)

*merge in inventory the day before 7/1/2015
gen date630=20270-1
mmerge inventoryid salesloc salesorg date630  using  "inventory_by_date.dta", type(n:1) unmatched(master) umatch(inventoryid salesloc salesorg date) ukeep(weight_at_start_of_day) urename(weight_at_start_of_day invweight0630)
cd "$dataclean"

gen ppgl1=pricepergramwh<1
gen ppglp5=pricepergramwh<.5
drop if pricepergramwh<1 

gen round1=round(pricepergramwh,1)==pricepergramwh
gen round1_whole=round(totalsaleswh,1)==totalsaleswh

*pctgapbysaleday pctgapbysaleday_adjusted
collapse (sum) sale totalsaleswh usableweightwh verticalweight (mean) round1 round1_whole invweightdaybefore invweight0630 gap meanweight ppgl1 ppglp5 dropclean procorg prodorg maxpricepergramwh* meanmaxweight pricepergramwh pricepergramwh_wgt logpricewh logpricewh_wgt  potency_thctotal potency_cbd  childweekly,  by(monthly salesloc procloc strains vertical inventorytype) fast
*inventoryid 
 
cd "${dataclean}"
save collapsed_wholesale_clean_forfirstdiffincidence_monthly.dta, replace /*collapses on inventoryid*/

*now create final monthly data set for anal: 
set more off
cd "${dataclean}"
use  collapsed_wholesale_clean_forfirstdiffincidence_monthly.dta, clear

keep if inventorytype==1
 
gen TaxChange=0
replace TaxChange=1 if monthly>=666
gen run=monthly-666
gen runtax=run*TaxChange

 
gen Placebo=monthly>=666+12
gen prun=monthly-(666+12)
gen prunplacebo=prun*Placebo


egen firstsaledate=min(monthly), by(procloc)
egen lastsaledate=max(monthly), by(procloc)


egen group_s=group(strains)

*restriction to eliminate mixed strains
count if (strains=="mixed"|strains=="unknown")&abs(run)<=2
count if abs(run)<=2 
drop if strains=="mixed"|strains=="unknown" 


***SET UP FIRST DIFF ANAL: 
* egen firstparentlotdate=min(weekly), by(parentlot)
egen groupx=group(salesloc procloc strains vertical )
*parentlot 
drop if groupx==.
xtset groupx monthly
gen logmaxprice=log(maxpricepergramwh)
 
gen taxdiff=TaxChange-L.TaxChange 

gen FTaxChange=monthly>=667
gen Ftaxdiff=FTaxChange-L.FTaxChange

gen F2TaxChange=monthly>=668
gen F2taxdiff=F2TaxChange-L.F2TaxChange 

gen F3TaxChange=monthly>=669
gen F3taxdiff=F3TaxChange-L.F3TaxChange 

 gen LTaxChange=monthly>=665
gen LTaxdiff=LTaxChange-L.LTaxChange 

 gen L2TaxChange=monthly>=664
gen L2Taxdiff=L2TaxChange-L.L2TaxChange 

 gen L3TaxChange=monthly>=663
gen L3Taxdiff=L3TaxChange-L.L3TaxChange 

gen F4TaxChange=monthly>=670
gen L4group=monthly<=662

  gen  placebodiff=Placebo-L.Placebo 
 
  gen pricediff5=pricepergramwh_wgt-L.pricepergramwh_wgt  

  
 gen pricediff6=logpricewh_wgt-L.logpricewh_wgt  

  gen weight5 =usableweight+L.usableweightwh  
  
  xtset groupx monthly
gen FPlacebo=monthly>=667+12
gen Fplacebodiff=FPlacebo-L.FPlacebo
replace Fplacebodiff=FPlacebo-L2.FPlacebo if Fplacebodiff==.
replace Fplacebodiff=FPlacebo-L3.FPlacebo if Fplacebodiff==.
replace Fplacebodiff=FPlacebo-L4.FPlacebo if Fplacebodiff==.

gen F2Placebo=monthly>=668+12
gen F2placebodiff=F2Placebo-L.F2Placebo
replace F2placebodiff=F2Placebo-L2.F2Placebo if F2placebodiff==.
replace F2placebodiff=F2Placebo-L3.F2Placebo if F2placebodiff==.
replace F2placebodiff=F2Placebo-L4.F2Placebo if F2placebodiff==.

 gen LPlacebo=monthly>=665+12
gen Lplacebodiff=LPlacebo-L.LPlacebo 
replace Lplacebodiff=LPlacebo-L2.LPlacebo if Lplacebodiff==.
replace Lplacebodiff=LPlacebo-L3.LPlacebo if Lplacebodiff==.
replace Lplacebodiff=LPlacebo-L4.LPlacebo if Lplacebodiff==.

 gen L2Placebo=monthly>=664+12
gen L2placebodiff=L2Placebo-L.L2Placebo 
replace L2placebodiff=L2Placebo-L2.L2Placebo if L2placebodiff==.
replace L2placebodiff=L2Placebo-L3.L2Placebo if L2placebodiff==.
replace L2placebodiff=L2Placebo-L4.L2Placebo if L2placebodiff==.

 gen L3Placebo=monthly>=663+12
gen L3placebodiff=L3Placebo-L.L3Placebo  
replace L3placebodiff=L3Placebo-L2.L3Placebo  if L3placebodiff==.
replace L3placebodiff=L3Placebo-L3.L3Placebo  if L3placebodiff==.
replace L3placebodiff=L3Placebo-L4.L3Placebo  if L3placebodiff==.

gen F3Placebo=monthly>=669+12
gen F3placebodiff=F3Placebo-L.F3Placebo 
replace F3placebodiff=F3Placebo-L2.F3Placebo if F3placebodiff==.
replace F3placebodiff=F3Placebo-L3.F3Placebo if F3placebodiff==.
replace F3placebodiff=F3Placebo-L4.F3Placebo if F3placebodiff==.

gen F4Placebo=monthly>=670+12
gen F4placebodiff=F4Placebo-L.F4Placebo 
replace F4placebodiff=F4Placebo-L2.F4Placebo if F4placebodiff==.
replace F4placebodiff=F4Placebo-L3.F4Placebo if F4placebodiff==.
replace F4placebodiff=F4Placebo-L4.F4Placebo if F4placebodiff==.

rename L3Taxdiff L3taxdiff
rename L2Taxdiff L2taxdiff
rename LTaxdiff Ltaxdiff

gen L4placebogroup=monthly<=662+12

cd "${dataclean}"
save incidence_procanal_firstdiff_monthly.dta, replace


